package cn.edu.sgu.www.mhxysy.controller.chongwu;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.chongwu.ChongwuDTO;
import cn.edu.sgu.www.mhxysy.dto.zuoqi.ZuoqiChongwuDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu.Chongwu;
import cn.edu.sgu.www.mhxysy.pager.chongwu.ChongwuPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.chongwu.ChongwuService;
import cn.edu.sgu.www.mhxysy.vo.chongwu.ChongwuVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "宠物管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/chongwu", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class ChongwuController {

	private final ChongwuService chongwuService;

	@Autowired
	public ChongwuController(ChongwuService chongwuService) {
		this.chongwuService = chongwuService;
	}

	@ApiOperation("计算宠物评分")
	@RequestMapping(value = "/compute", method = RequestMethod.POST)
	public JsonResult<Integer> compute(
			@ApiParam(name = "chongwuId", value = "宠物ID", required = true)
			@NotBlank(message = CustomMessages.CHONGWU_ID_NOT_NULL) String chongwuId) {
		Integer compute = chongwuService.compute(chongwuId);

		return JsonResult.success(CustomMessages.OPERATE_SUCCESS, compute);
	}

	@ApiOperation("更换统御")
	@RequestMapping(value = "/change", method = RequestMethod.POST)
	public JsonResult<Void> change(@Validated ZuoqiChongwuDTO zuoqiChongwuDTO) {
		chongwuService.change(zuoqiChongwuDTO);

		return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
	}

	@ApiOperation("统御宠物")
	@RequestMapping(value = "/tongyu", method = RequestMethod.POST)
	public JsonResult<Void> tongyu(@Validated ZuoqiChongwuDTO zuoqiChongwuDTO) {
		chongwuService.tongyu(zuoqiChongwuDTO);

		String[] operationArray = {"统御", "取消统御"};
		Integer tongyuStatus = zuoqiChongwuDTO.getTongyuStatus();

		return JsonResult.success(operationArray[tongyuStatus] + "成功");
	}

	@ApiOperation("添加宠物")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated ChongwuDTO chongwuDTO) {
		chongwuService.insert(chongwuDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID删除宠物")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(
			@ApiParam(name = "id", value = "宠物ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		chongwuService.deleteById(id);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改宠物信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated ChongwuDTO chongwuDTO) {
		chongwuService.updateById(chongwuDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询全部宠物")
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<Chongwu>> selectAll() {
		List<Chongwu> list = chongwuService.selectAll();

		return JsonResult.success(null, list);
	}

	@ApiOperation("通过ID查询宠物信息")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<Chongwu> selectById(
			@ApiParam(name = "id", value = "宠物ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		Chongwu chongwu = chongwuService.selectById(id);

		return JsonResult.success(null, chongwu);
	}

	@ApiOperation("通过角色ID查询宠物列表")
	@RequestMapping(value = "/selectByRoleId", method = RequestMethod.GET)
	public JsonResult<List<Chongwu>> selectByRoleId(
			@ApiParam(name = "roleId", value = "角色ID", required = false)
			@RequestParam(required = false) String roleId) {
		List<Chongwu> list = chongwuService.selectByRoleId(roleId);

		return JsonResult.success(null, list);
	}

	@ApiOperation("分页查询宠物列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
	public JsonResult<JsonPage<Chongwu>> selectByPage(ChongwuPager pager) {
		Page<Chongwu> page = chongwuService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

	@ApiOperation("通过坐骑ID查询宠物信息")
	@RequestMapping(value = "/selectByZuoqiId", method = RequestMethod.GET)
	public JsonResult<List<ChongwuVO>> selectImagesByZuoqiId(
			@ApiParam(name = "zuoqiId", value = "坐骑ID", required = true)
			@NotBlank(message = CustomMessages.ZUOQI_ID_NOT_NULL) String zuoqiId) {
		List<ChongwuVO> list = chongwuService.selectByZuoqiId(zuoqiId);

		return JsonResult.success(null, list);
	}

}